<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 15:22
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Wish;
use app\common\model\WishLog;
use app\common\model\WishLogs;
use app\common\model\User;
use app\vehicle\logic\TplLogic;
class WishLogic extends BaseLogic
{

    private $WishModel;

    public function __construct()
    {
        parent::__construct();
        $this->WishModel = new Wish();
    }

    public function applyList($tempWhere, $nowPage){
        $where[] = ['is_del','eq',0];
        $where[] = ['wish_id', '=', $tempWhere['id']];
        $count = WishLog::where($where)->count();
        if ($count > 0){
            $articleList = WishLog::where($where)->order('id desc')-field('*,create_time as c_time,suc_time as s_time,cover_time as co_time')>page($nowPage, $tempWhere['limit'])->select();
            foreach($articleList as $k=>$v){
                $user = User::where('id','eq',$v['user_id'])->find();
                $articleList[$k]['nickname'] = $user['nick_name'];
                $articleList[$k]['c_time'] = date('Y-m-d H:i',$v['c_time']);
                $articleList[$k]['s_time'] = $v['s_time']?date('Y-m-d H:i',$v['s_time']):'';
                $articleList[$k]['co_time'] = $v['co_time']?date('Y-m-d H:i',$v['co_time']):'';
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $articleList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function WishApplyList($tempWhere, $nowPage){
        $where[] = ['is_del','eq',0];
        $tempWhere['title'] && $where[] = ['w.title', 'like', "%".$tempWhere['title']."%"];
        $tempWhere['realname'] && $where[] = ['w.realname', 'like', "%".$tempWhere['realname']."%"];
        $count = WishLog::alias('as al')
            ->join('wish w','al.wish_id=w.id')
            ->join('user u','al.user_id=u.id')
            ->where($where)
            ->count();
        if ($count > 0){
            $articleList = WishLog::alias('as al')
                ->join('wish w','al.wish_id=w.id')
                ->join('user u','al.user_id=u.id')
                ->where($where)
                ->order('al.id desc')
                ->field('al.*,al.create_time as c_time,al.suc_time as s_time,al.cover_time as co_time,w.title,w.realname as real_name,w.v_mobile,w.content,u.nick_name as nickname,u.head_pic')
                ->page($nowPage, $tempWhere['limit'])
                ->select();
            foreach($articleList as $k=>$v){
                $v['content'] = htmlspecialchars_decode($v['content']);
                $articleList[$k]['c_time'] = date('Y-m-d H:i',$v['c_time']);
                $articleList[$k]['s_time'] = $v['s_time']?date('Y-m-d H:i',$v['s_time']):'';
                $articleList[$k]['co_time'] = $v['co_time']?date('Y-m-d H:i',$v['co_time']):'';
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $articleList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    public function WishList($tempWhere, $nowPage){
        if ($tempWhere['title']){
            $where[] = ['title', 'like', "%".$tempWhere['title']."%"];
        }
        if ($tempWhere['realname']){
            $where[] = ['realname', 'like', "%".$tempWhere['realname']."%"];
        }
        if (is_numeric($tempWhere['order_status'])){
            $where[] = ['order_status', 'eq', $tempWhere['order_status'] ];
        }
        if (is_numeric($tempWhere['status'])){
            $where[] = ['status', 'eq', $tempWhere['status'] ];
        }
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        $site_id && $where[] = ['sid', '=', $site_id];
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        $oid && $where[] = ['sid', '=', $oid];

        $count = $this->WishModel->where($where)->count();
        if ($count > 0){
            $WishList = $this->WishModel->where($where)->order('id desc')->page($nowPage, $tempWhere['limit'])->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $WishList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    /**
     * 查找文章信息
     * @param $WishId
     * @return mixed
     */
    public function info($WishId){
        $info = $this->WishModel->field('*,create_time as c_time')->get($WishId);
        $info['c_time'] = date('Y-m-d H:i:s',$info['c_time']);
        if ($info){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '该文章不存在');
        }
    }

    /**
     * 文章逻辑操作
     * @param $data
     * @return mixed
     */
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        if(!$data['thumb']){
            unset($data['thumb']);
        }
        $data['use_time'] = strtotime($data['use_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['create_time'] = strtotime($data['create_time']);
        switch ($act){
            case 'add':
                $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
                $site_id && $data['sid'] = $site_id;
                $oid = $_SESSION['xm_travel']['admin_info']['oid'];
                $oid && $data['sid'] = $oid;
                $data['content'] = htmlspecialchars($data['content']);
                $res = $this->WishModel->allowField(true)->save($data);
                break;
            case 'edit':
                $info = $this->WishModel->where('id',$data['id'])->find();
                $openid = User::where('id','eq',$info['user_id'])->value('open_id');
                if($data['status']==1){
                    $data['examine_time'] = time();
                    $openid && TplLogic::examineTpl($openid,'发布心愿','审核通过');
                }else if($data['status']==2){
                    $openid && TplLogic::examineTpl($openid,'发布心愿','审核不通过');
                }
                $data['content'] = htmlspecialchars($data['content']);
                $res = $this->WishModel->allowField(true)->save($data, $data['id']);
                break;
            case 'del':
                $Wish = $this->WishModel->get($data['id']);
                $res = $Wish->delete();
                break;
            case 'del_apply':
                $info = WishLog::where('id',$data['id'])->find();
                Wish::where('id','eq',$info['wish_id'])->update(['order_status'=>0]);
                WishLogs::insert([
                    'user_id'=>$info['user_id'],
                    'wish_id'=>$info['wish_id'],
                    'realname'=>$info['realname'],
                    'mobile'=>$info['mobile'],
                    'create_time'=>strtotime($info['create_time'])
                ]);
                $res = WishLog::where('id',$data['id'])->delete();
                break;
            case 'batch':
                $res = $this->WishModel->where('id','in',$data['ids'])->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

}